﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Resources;
using Service;


public partial class employeeView : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {

        // set refresh timer 
        Timer1.Enabled = false;

        txtHiddenEmployeeId.Text = Request.QueryString["employeeId"];
        if (Session["whereClause"] == null)
            Session["whereClause"] = txtHiddenWhereClause.Text;
        if (Session["orderByClause"] == null)
            Session["orderByClause"] = txthiddenOrderbyClause.Text;
        grdEmployee.PageIndex = !String.IsNullOrEmpty(txtHiddenPageIndex.Text) ? Int32.Parse(txtHiddenPageIndex.Text) : 0;

        new Utility().SetRowToDisplay(grdEmployee, Session["rowsToSDisplay"]);
        if (!IsPostBack)
        {

            // to avoid loading for the first time

           
            if ((Request.QueryString["detail"] == null))
            {
                grdGroupeSoftware1.Visible = true; 
                if ((Request.QueryString["delete"] == null))
                {
                    txtHiddenWhereClause.Text = "and Employee.EmployeeId='0'";
                    lblTotalEmployee.Text = "Total Employees : 0";
                }
                else
                {
                    int equipmentId = Request.QueryString["employeeId"] != null ? Int32.Parse(Request.QueryString["employeeId"]) : 0;
                    new StockService().DeleteEmployee(equipmentId);
                    txtHiddenWhereClause.Text = Session["whereClause"].ToString();
                    txthiddenOrderbyClause.Text = Session["orderByClause"].ToString();
                    lblTotalEmployee.Text = "Total Employees : " + new DirectService().GetEmployees(txtHiddenWhereClause.Text).Rows.Count;
                }
            }
            else
            {
                txtHiddenWhereClause.Text = Session["whereClause"].ToString();
                txthiddenOrderbyClause.Text = Session["orderByClause"].ToString();
                lblTotalEmployee.Text = "Total Employees : " + new DirectService().GetEmployees(txtHiddenWhereClause.Text).Rows.Count;
                
            }


            if (Session["pageIndex"] != null)
            {
                grdEmployee.PageIndex = Int32.Parse(Session["pageIndex"].ToString());

            }


            /* 
             * 1. number of equipments
             * 2. select the element 
             * */
           
        }
        else
        {
            txtHiddenPageIndex.Text = "0";
            lblTotalEmployee.Text = "Total Employees : " + new DirectService().GetEmployees(GetWhereClause()).Rows.Count;
            int number = +new DirectService().GetEmployees(GetWhereClause()).Rows.Count;
            if (number == 0)
            {
                grdGroupeSoftware1 .Visible = true;
            }
            else
            {
                grdEmployee.Visible = true;
                grdGroupeSoftware1.Visible = false;
            }
        }
    }
    public int GetPageIndex()
    {
        return Session["pageIndex"] != null
                   ?
                       Int32.Parse(Session["pageIndex"].ToString())
                   : 0;
    }
    public int GetTabIndex()
    {
        int tabIndex = Request.QueryString["tabIndex"] != null
                        ?
                            Int32.Parse(Request.QueryString["tabIndex"])
                        : 0;
        if (Session["defaultTabIndex"] != null)
        {
            tabIndex = Int32.Parse(Session["defaultTabIndex"].ToString());
            Session["defaultTabIndex"] = null;
        }

        return tabIndex;
    }

   
   

    public int GetEmployeeId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["employeeId"]) ? Int32.Parse(Request.QueryString["employeeId"]) : 0;
    }
    private void InitializeSearchData()
    {
        ReferenceDataService referenceService  = new ReferenceDataService();
        drdGroup.DataSource = referenceService.GetUserGroups();
        drdGroup.DataTextField = Resources.Resource.UserGroupTextField;
        drdGroup.DataValueField = Resources.Resource.UserGroupValueField;
        drdGroup.DataBind();

        drdDepartment.DataSource = referenceService.GetDepartments();
        drdDepartment.DataTextField = Resources.Resource.DepartmentTextField;
        drdDepartment.DataValueField = Resources.Resource.DepartmentValueField;
        drdDepartment.DataBind();
    }
    private void GetSearchSession()
    {
        //if (!Session["departmentId"].Equals("-1")) 
        Session["departmentId"] = drdDepartment.SelectedValue;
        //if (!Session["groupId"].Equals("-1")) 
        Session["groupId"] = drdGroup.SelectedValue;
        

    }
    protected string GetOrderClause()
    {
        return " Order by Employee.EmployeeName";
    }

    protected void grdEmployee_SelectedIndexChanged(object sender, EventArgs e)
    {
        txtHiddenPageIndex.Text = grdEmployee.PageIndex.ToString();
    }
    protected void grdEmployee_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grdEmployee_PageIndexChanged1(object sender, EventArgs e)
    {

    }
    protected void grdEmployee_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdEmployee.PageIndex = e.NewPageIndex;
        txtHiddenPageIndex.Text = grdEmployee.PageIndex.ToString();
        Session["pageIndex"] = grdEmployee.PageIndex;
    }
    protected void grdEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdEmployee.EditIndex = -1;

    }
    protected void grdEmployee_RowDataBound1(object sender, GridViewRowEventArgs e)
    {

    }
    protected void grdEmployee_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdEmployee.EditIndex = e.NewEditIndex;

    }
    protected void bntFilter_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionViewEmployee))
        {
            lblPermission.Text = Resource.Permission;
            lblTotalEmployee.Visible = false;
            return;
        }
        lblPermission.Visible = false;
        lblTotalEmployee.Visible = true;
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;

        txtEmployeeName.Text = string.Empty;  
    }
    private void GetPreviouSearchSession()
    {
        drdDepartment.SelectedValue = Session["departmentId"] != null ? Session["departmentId"].ToString() : "-1";
        drdGroup.SelectedValue = Session["groupId"] != null ? Session["groupId"].ToString() : "-1";
        
    }
    protected string GetWhereClause()
    {
        string whereClause = string.Empty;
        string and = "";
        string str = "";

        //prepare by system
        if (drdGroup.SelectedIndex > 0)
        {
            str += and + "EMPLOYEE.IDGROUPE=" + drdGroup.SelectedValue;
            and = " and ";
        }

        //prepare by type equipement
        if (drdDepartment.SelectedIndex > 0)
        {
            str += and + "EMPLOYEE.departmentID=" + drdDepartment.SelectedValue;
            and = " and ";
        }

        //prepare by type equipement
        if (!String.IsNullOrEmpty(txtEmployeeName.Text))
        {
            str += and + "EMPLOYEE.EMPLOYEENAME like '%" + txtEmployeeName.Text+"%' ";
            and = " and ";
        }
        if (drlCompanyName.SelectedIndex > 0)
        {
            str += and + "EMPLOYEE.CompanyID=" + drlCompanyName.SelectedValue;
            and = " and ";
        }
        if (!String.IsNullOrEmpty(str))
            whereClause = " and " + str + " ";
        return whereClause;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Employees.DataBind();
    }

    protected void Employees_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (e.ReturnValue != null) lblTotalEmployee.Text = "Total employee:" + ((DataTable)e.ReturnValue).Rows.Count;
    }
    protected void grdEmployee_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        //if (e.CommandName.Equals("Select"))
        //{
        //    int employeeId = Convert.ToInt32(grdEmployee.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
        //    Response.Redirect("employeeView.aspx?&employeeId=" + employeeId);
        //}

        new Utility().SetSelectedColorToGridView(e, grdEmployee);
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (Session["defaultTabIndex"] != null)
            Timer1.Enabled = Session["defaultTabIndex"].ToString().Equals("0") ? false : true;
        Session["whereClause"] = GetWhereClause();
        Session["orderByClause"] = GetOrderClause();
        txthiddenOrderbyClause.Text = GetOrderClause();
        txtHiddenWhereClause.Text = GetWhereClause();
        Session["defaultTabIndex"] = 0;

        txtEmployeeName.Text = string.Empty;
    }
}

   
